package handler

import (
	"context"
	"github.com/google/uuid"
	"shop-server/basic/config"
	"shop-server/internal/model"
	__ "shop-server/proto"
)

type ShopServer struct {
	__.UnimplementedShopServer
}

func (s *ShopServer) Create(_ context.Context, req *__.CreateReq) (*__.CreateResp, error) {
	//var shop model.Shop
	order := model.Shop{
		ShopName: req.ShopName,
		Num:      uint(req.Num),
		Price:    float64(req.Price),
		Details:  req.Details,
	}
	var user model.User
	config.DB.Where("id = ?", user.Id).First(&user)
	if user.Id != 0 {
		return &__.CreateResp{
			Code:    10000,
			Message: "用户不存在",
		}, nil
	}
	config.DB.Create(&order)
	return &__.CreateResp{
		Id:      int64(order.Id),
		Code:    200,
		Message: "添加成功",
	}, nil

}

func (s *ShopServer) ListOrder(_ context.Context, req *__.ListOrderReq) (*__.ListOrderResp, error) {
	var order []model.Shop

	office := (req.Page + 1) - req.SizePage
	config.DB.Offset(int(office)).Limit(1).First(&order)

	listShop := []*__.ListOrder{}
	for _, shop := range order {
		listShop = append(listShop, &__.ListOrder{
			ShopName: shop.ShopName,
			Num:      int64(shop.Num),
			Price:    float32(shop.Price),
			Details:  shop.Details,
		})
		return nil, nil
	}
	return &__.ListOrderResp{
		List: listShop,
	}, nil
}

func (s *ShopServer) Register(_ context.Context, req *__.RegisterReq) (*__.RegisterResp, error) {
	var user model.User
	config.DB.Where("mobile = ?").First(&user)
	if user.Id != 0 {
		return &__.RegisterResp{
			Code:    1000,
			Message: "用户以存在",
		}, nil
	}
	config.DB.Create(&user)
	return &__.RegisterResp{
		Code:    200,
		Message: "注册成功",
	}, nil
}

func (s *ShopServer) Login(_ context.Context, req *__.LoginReq) (*__.LoginResp, error) {
	var user model.User
	config.DB.Where("mobile = ?").First(&user)
	if user.Id == 0 {
		return &__.LoginResp{
			Code:    1000,
			Message: "用户以存在",
		}, nil
	}
	return &__.LoginResp{
		Code:    200,
		Message: "注册成功",
	}, nil

}

func (s *ShopServer) ShowShop(_ context.Context, req *__.ShowShopReq) (*__.ShowShopResp, error) {
	var shop model.Shop
	err := config.DB.Where("id = ?", req.Id).Find(&shop)
	if err != nil {
		panic(err)
	}
	if shop.Id == 0 {
		return &__.ShowShopResp{
			Code:    1000,
			Message: "商品不存在",
		}, nil
	}
	return &__.ShowShopResp{
		ShopName: shop.ShopName,
		Num:      int64(shop.Num),
		Price:    float32(shop.Price),
		Details:  shop.Details,
	}, nil
}
func (s *ShopServer) AddOrder(_ context.Context, req *__.AddOrderReq) (*__.AddOrderResp, error) {
	//var shop model.Shop
	var user model.User
	config.DB.Where("id = ?", user.Id).First(&user)
	if user.Id != 0 {
		return &__.AddOrderResp{
			Code:    10000,
			Message: "用户不存在",
		}, nil
	}
	var shop model.Shop
	config.DB.Where("id = ?", shop.Id).First(&shop)
	if user.Id != 0 {
		return &__.AddOrderResp{
			Code:    10000,
			Message: "商品不存在",
		}, nil
	}
	number := uuid.NewString()
	order := model.Order{
		OrderNumber: number,
		ShopId:      uint(req.ShopId),
		UserId:      uint(req.UserId),
		Address:     req.Address,
		TotalPrice:  float64(req.TotalPrice),
	}
	//if order.OrderNumber == req.OrderNumber {
	//	return &__.AddOrderResp{
	//		Code:    10000,
	//		Message: "订单号重复",
	//	}, nil
	//}

	config.DB.Create(&order).Where("userid = ? && shopid = ?", user.Id, shop.Id).Delete(&user.Id, shop.Id)
	return &__.AddOrderResp{
		Code:    200,
		Message: "添加成功",
	}, nil

}
